<?php


namespace app\admin\controller\Banner;

use app\common\controller\Adminbase;
use think\Db;

class Banner extends Adminbase
{
//    protected $searchFields = 'id,username,nickname';

    protected function initialize()
    {

        parent::initialize();
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            list($page, $limit) = $this->buildTableParames();
            $filter = input('filter');
            if ($filter) {
                $filter = json_decode($filter, true);
                if (isset($filter['id'])) {
                    $where[] = ['id', '=',  $filter['id'] ];
                }
                if (isset($filter['status'])) {
                    $where[] = ['status', '=',  $filter['status'] ];
                }
                if (isset($filter['province'])) {
                    $province = Db::name('area')->find($filter['province']);
                    $where[] = ['province', '=', $province['name']];
                }
                if (isset($filter['city'])) {
                    $city = Db::name('area')->find($filter['city']);
                    $where[] = ['city', '=', $city['name']];
                }
            }
            $_list = Db::name('banner')
                ->page($page, $limit)
                ->where($where)
                ->order('sort desc')->select();
            $total = Db::name('banner')->where($where)->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        $area_list=Db::name('area')->where([
            ['id','<>',0],
            ['pid','=',0]
        ])->select();
        foreach ($area_list as $k=>$v){
            $proviec[$v['id']]=$v['name'];
        }
        $this->assign('proviec',$proviec);
        return $this->fetch();
    }

    /**
     * 状态
     */
    public function setstate()
    {
        $id = $this->request->param('id/d', 0);

        $status = $this->request->param('value/d');


        if (Db::name('banner')->where('id', $id)->update(['status' => $status])) {

            $this->success('操作成功！');
        } else {
            $this->error('操作失败！');
        }
    }

    //删除
    public function del()
    {

        $ids = $this->request->param('ids/a', null);

        try {
            foreach ($ids as $id) {
                Db::name('banner')->where('id', $id)->delete();
            }
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('删除成功！');
    }

    //增加
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $province=Db::name('area')->find( $data['province']);
            $city=Db::name('area')->find( $data['city']);
            $insert_data = array(
                'title' => $data['title'],
                'province' => $province['name'],
                'city' => $city['name'],
                'province_id' => $data['province'],
                'city_id' => $data['city'],
                'page' => $data['page'],
                'img' => $data['img'],
                'sort' => $data['sort'],
                'status' => 1
            );
            $stmt = Db::name('banner')->insertGetId($insert_data);
            if ($stmt) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
        } else {
            $area_list = Db::name('area')->where([
                ['id', '<>', 0],
                ['pid', '=', 0]
            ])->select();
            $this->assign('proviec',$area_list);
            return $this->fetch();
        }
    }

    public function edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->Post();
            $province=Db::name('area')->find( $data['province']);
            $city=Db::name('area')->find( $data['city']);
            $update_data = array(
                'id' => $data['id'],
                'title' => $data['title'],
                'img' => $data['img'],
                'sort' => $data['sort'],
                'page' => $data['page'],
                'province' => $province['name'],
                'city' => $city['name'],
                'province_id' => $data['province'],
                'city_id' => $data['city'],
            );
            $stmt = Db::name('banner')->update($update_data);
            if ($stmt) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
            return $data;
        }
        $id = $this->request->param('id/d', 0);

        $area_list = Db::name('area')->where([
            ['id', '<>', 0],
            ['pid', '=', 0]
        ])->select();
        $data = Db::name('banner')->find($id);
        //获取市
        $city_list=Db::name('area')->where([
            'pid'=>$data['province_id']
        ])->select();
        $this->assign([
            'data'=>$data,
            'city_list'=>$city_list,
            'proviec'=>$area_list
        ]);
        return $this->fetch();
    }

    public function getc_cate()
    {
        $id = input('id');
        $query = db('area')->where('pid', $id)->select();
        return json(['code' => 200, 'data' => $query, 'msg' => '获取成功']);
    }

}